  <template>
  <el-row type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px;  width: 100%;">
    <el-col :span="20">
      <i v-if="root"
         class="el-icon-office-building" />
      <span style="font-size: 18px; font-weight: 500:">{{nodeData.name}}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex"
              justify="end">
        <el-col class="box"
                :title="nodeData.manager">{{nodeData.manager}}</el-col>
        <el-col class="box">
          <el-dropdown @command="commanFn">
            <span class="el-dropdown-link"
                  @click.stop>
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                <el-dropdown-item v-if="!root"
                                  command="edit">查看部门</el-dropdown-item>
                <el-dropdown-item v-if="!root"
                                  command="del">删除部门</el-dropdown-item>
              </el-dropdown-menu>
            </template>

          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartment } from '@/api/departments'
export default {
  name: 'TreeTools',
  props: {
    nodeData: {
      type: Object,
      required: true
    },
    root: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // element-ui 的属性
    commanFn(command) {
      switch (command) {
        case 'add':
          this.add()
          break
        case 'edit':
          this.edit()
          break
        case 'del':
          this.del()
          break
      }
    },
    add() {
      this.$emit('add', this.nodeData)  // 子传父 改掉编辑框的状态为true
    },
    edit() {
      this.$emit('edit', this.nodeData)
    },
    async del() {
      try {
        await this.$confirm('你确定要删除吗', '温馨提示', { type: 'warning' })
      } catch {
        return
      }
      await delDepartment(this.nodeData.id)
      this.$message.success('删除成功')
      // 子传fu 更新
      this.$emit('getDepartments')
    }

  }
}
</script>

<style lang="scss" scoped>
.box {
  width: 80px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
::v-deep {
  .el-icon-office-building {
    margin-right: 5px;
    font-size: 20px;
  }
}
</style>
